package cz.myslivec.slovicka.action;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;

import cz.myslivec.slovicka.entity.Lesson;
import cz.myslivec.slovicka.entity.TestedVocable;
import cz.myslivec.slovicka.entity.Vocable;
import cz.myslivec.slovicka.entity.VocableType;

@Name("test001")
@AutoCreate
public class Test001 implements Serializable {
	private static final long serialVersionUID = 7273737110990941571L;

	@Logger
	private Log log;
	
	@In
	private EntityManager entityManager;

	public void execute() {
		createDB01();
		printDB();
	}
	
	public void createDB01() {
		// VOCABLE TYPE
		VocableType vocType = new VocableType();
		vocType.setCode("AJ/CJ");
		vocType.setDescription("cesko anglicka slovicka");
		vocType.setStr1("anglicky");
		vocType.setStr2("cesky");
		entityManager.persist(vocType);

		// LESSON
		Lesson lesson = new Lesson();
		lesson.setName("lekce 1");
		lesson.setNote("lekce 1 - poznamka");
		lesson.setVocType(vocType);
		entityManager.persist(lesson);
		
		Lesson lesson2 = new Lesson();
		lesson2.setName("lekce 2");
		lesson2.setNote("lekce 2 - poznamka");
		lesson2.setVocType(vocType);
		entityManager.persist(lesson2);
		
		
		// VOCABLE
		Vocable vocable1 = new Vocable();
		vocable1.setStr1("hello");
		vocable1.setStr2("ahoj");
		entityManager.persist(vocable1);
		vocable1.setLessons(Arrays.asList(lesson));
		lesson.addVocable(vocable1);
				
		Vocable vocable2 = new Vocable();
		vocable2.setStr1("Hi");
		vocable2.setStr2("Cau");
		entityManager.persist(vocable2);
		vocable2.setLessons(Arrays.asList(lesson));
		lesson.addVocable(vocable2);
		
		Vocable vocable3 = new Vocable();
		vocable3.setStr1("Bed");
		vocable3.setStr2("postel");
		entityManager.persist(vocable3);
		vocable3.setLessons(Arrays.asList(lesson2));
		lesson2.addVocable(vocable3);
		
		// TESTED VOCABLE
		Calendar cal = Calendar.getInstance();
		
		TestedVocable tv1 = new TestedVocable();
		cal.set(2013, 01, 10, 13, 30);
		tv1.setTestDate(cal.getTime());
		tv1.setSuccessLevel(4);
		tv1.setVocable(vocable1);
		entityManager.persist(tv1);
		
		TestedVocable tv2 = new TestedVocable();
		cal.set(2013, 01, 11, 15, 30);
		tv2.setTestDate(cal.getTime());
		tv2.setSuccessLevel(2);
		tv2.setVocable(vocable1);
		entityManager.persist(tv2);
		
		TestedVocable tv3 = new TestedVocable();
		cal.set(2013, 01, 11, 15, 45);
		tv3.setTestDate(cal.getTime());
		tv3.setSuccessLevel(1);
		tv3.setVocable(vocable2);
		entityManager.persist(tv3);
		
		entityManager.flush();
	}
	
	public void printDB() {
		printLessons();
		printVocType();
		printVocables();
		printTestedVocables();
		printLesVoc();
	}
	
	@SuppressWarnings("unchecked")
	public void printLessons() {
		log.info("--- LESSONS ---");
		log.info("ID  | NAME | NOTE | VOC_TYPE");
		
		Query q1 = entityManager.createQuery("FROM Lesson");
		List<Lesson> lessons = (List<Lesson>) q1.getResultList();
		if(lessons != null && !lessons.isEmpty()) {
			for(Lesson lesson : lessons) {
				log.info("{0} | {1} | {2} | {3}", lesson.getId(), lesson.getName(), lesson.getNote(), lesson.getVocType().getCode());
			}
		}
		
		log.info("");
	}
	
	@SuppressWarnings("unchecked")
	public void printVocType() {
		log.info("--- VOC TYPES ---");
		log.info("ID  | NAME | STR1 | STR2 | STR3 | STR4 | DESCRIPTION");
		
		Query q1 = entityManager.createQuery("FROM VocableType");
		List<VocableType> vocTypes = (List<VocableType>) q1.getResultList();
		if(vocTypes != null && !vocTypes.isEmpty()) {
			for(VocableType vocType : vocTypes) {
				log.info("{0} | {1} | {2} | {3} | {4} | {5} ", vocType.getCode(), vocType.getStr1(), vocType.getStr2(), vocType.getStr3(), vocType.getStr4(), vocType.getDescription());
			}
		}
		
		log.info("");
	}
	
	@SuppressWarnings("unchecked")
	public void printVocables() {
		log.info("--- VOCABLES ---");
		log.info("ID  | LESSONS | STR1 | STR2 | STR3 | STR4 ");
		
		Query q1 = entityManager.createQuery("FROM Vocable");
		List<Vocable> vocables = (List<Vocable>) q1.getResultList();
		if(vocables != null && !vocables.isEmpty()) {
			for(Vocable vocable : vocables) {
				log.info("{0} | {1} | {2} | {3} | {4} | {5} | {6}", vocable.getId(), convertLessonsToString(vocable.getLessons()), 
						vocable.getStr1(), vocable.getStr2(), vocable.getStr3(), vocable.getStr4());
			}
		}
		
		log.info("");
	}
	
	private String convertLessonsToString(List<Lesson> lessons) {
		if(lessons == null || lessons.isEmpty()) {
			return "";
		}
		StringBuilder sb = new StringBuilder(); 
		for(Lesson lesson : lessons)  {
			sb.append(lesson.getId()).append(" ");
		}
		return sb.toString();
	}
	
	@SuppressWarnings("unchecked")
	public void printTestedVocables() {
		log.info("--- TESTED VOCABLES ---");
		log.info("ID  | VOCABLE_ID| SUCCESS | DATE ");
		
		Query q1 = entityManager.createQuery("FROM TestedVocable");
		List<TestedVocable> testedVocables = (List<TestedVocable>) q1.getResultList();
		if(testedVocables != null && !testedVocables.isEmpty()) {
			for(TestedVocable testedVocable : testedVocables) {
				log.info("{0} | {1} | {2} | {3} ", testedVocable.getId(), testedVocable.getVocable().getId(), testedVocable.getSuccessLevel(), testedVocable.getTestDate());
			}
		}
		
		log.info("");
	}
	
	@SuppressWarnings("unchecked")
	public void printLesVoc() {
		log.info("--- LESS_VOC ---");
		log.info("ID_LES | ID_VOC");
		
		Query q1 = entityManager.createNativeQuery("SELECT ID_LES, ID_VOC FROM LESS_VOC");
		List<Object[]> lessVocs = (List<Object[]>) q1.getResultList();
		if(lessVocs != null && !lessVocs.isEmpty()) {
			for(Object[] objects : lessVocs) {
				if(objects != null && objects.length > 0) {
					log.info("{0} | {1} ", objects[0], objects[1]);
				}
			}
		}
		
		log.info("");
	}

}
